using UnityEngine;
using System.Collections;
using COC.Character;
public class ScriptHuman : BaseBehaviourSupport
{
		[System.NonSerialized]
	
		public bool useCurves;
	    public float lookWeight;					// the amount to transition when using head look
		public float Speed = 0.5f;
		private Animator anim;
		public float animSpeed = 1.5f;				// a public setting for overall animator animation speed
		private AnimatorStateInfo currentBaseState;			// a reference to the current nodeState of the animator, used for base layer
		private AnimatorStateInfo layer2CurrentState;	// a reference to the current nodeState of the animator, used for layer 2
		private CapsuleCollider col;					// a reference to the capsule collider of the character
		static int idleState = Animator.StringToHash ("Base Layer.Idle");
		static int locoState = Animator.StringToHash ("Base Layer.Locomotion");			// these integers are references to our animator's states
		static int jumpState = Animator.StringToHash ("Base Layer.Jump");				// and are used to check nodeState for various actions to occur
		static int jumpDownState = Animator.StringToHash ("Base Layer.JumpDown");		// within our FixedUpdate() function below
		static int fallState = Animator.StringToHash ("Base Layer.Fall");
		static int rollState = Animator.StringToHash ("Base Layer.Roll");
		static int waveState = Animator.StringToHash ("Layer2.Wave");

	   //terrain object
	    public GameObject terrain;
		//enemy touched transform
		public GameObject  touchedCube;
	



		// Use this for initialization
		void Start ()
		{
				//enable terrain tree collider 
		if(terrain==null){
			GameObject go=GameObject.FindGameObjectWithTag("Terrain");
			terrain=go;
		}
		terrain.GetComponent<TerrainCollider>().enabled=true;

		Debug.Log("Terrain collider is :"+terrain.GetComponent<TerrainCollider>().enabled);



				// initialising reference variables
				anim = GetComponent<Animator> ();
				col = GetComponent<CapsuleCollider> ();				

				if (anim.layerCount == 2)
						anim.SetLayerWeight (1, 1);

		}
	
		// Update is called once per frame
		void FixedUpdate ()
		{
				float h = Input.GetAxis ("Horizontal");				// setup h variable as our horizontal input axis
				float v = Input.GetAxis ("Vertical");				// setup v variables as our vertical input axis
				anim.SetFloat ("Speed", v);							// set our animator's float parameter 'Speed' equal to the vertical input axis				
				anim.SetFloat ("Direction", h); 						// set our animator's float parameter 'Direction' equal to the horizontal input axis		
				anim.speed = animSpeed;								// set the speed of our animator to the public variable 'animSpeed'
				anim.SetLookAtWeight (lookWeight);
				currentBaseState = anim.GetCurrentAnimatorStateInfo (0);	// set our currentState variable to the current nodeState of the Base Layer (0) of animation
				if (anim.layerCount == 2)		
						layer2CurrentState = anim.GetCurrentAnimatorStateInfo (1);	// set our layer2CurrentState variable to the current nodeState of the second Layer (1) of animation

				if (currentBaseState.nameHash == locoState) {
						if (Input.GetButtonDown ("Jump")) {    //press space btn
								anim.SetBool ("Jump", true);
						}
				} else if (currentBaseState.nameHash == jumpState) {
						if (!anim.IsInTransition (0)) {
								col.height = anim.GetFloat ("ColliderHeight");
						}
						anim.SetBool ("Jump", false);
						// Raycast down from the center of the character.. 
						Ray ray = new Ray (transform.position + Vector3.up, -Vector3.up);
						RaycastHit hitInfo = new RaycastHit ();
			
						if (Physics.Raycast (ray, out hitInfo)) {
								// ..if distance to the ground is more than 1.75, use Match Target
								if (hitInfo.distance > 1.75f) {
					
										// MatchTarget allows us to take over animation and smoothly transition our character towards a location - the hit point from the ray.
										// Here we're telling the Root of the character to only be influenced on the Y axis (MatchTargetWeightMask) and only occur between 0.35 and 0.5
										// of the timeline of our animation clip
										anim.MatchTarget (hitInfo.point, Quaternion.identity, AvatarTarget.Root, new MatchTargetWeightMask (new Vector3 (0, 1, 0), 0), 0.35f, 0.5f);
								}
						}

				}
	
		}

		void OnTriggerEnter (Collider other)
		{
				if (other.gameObject.tag == "enemy") {
						if (touchedCube) {
								//print ("I Touched:"+other.name);
								//	}
								Instantiate (touchedCube, other.transform.position, other.transform.rotation);
								Destroy (other.gameObject);
						}
				}
		}

		void OnTriggerExit (Collider other)
		{
				print ("I touched recently:" + other.name);
//		if(other.tag=="enemyCube"){
//			Instantiate(touchedCube,other.transform.position,other.transform.rotation);
//		}
		}

		void OnTriggerStay (Collider other)
		{
				//	print("stay");
		}
}
